package com.myiam.idm.portal.beans.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.myiam.idm.core.model.Role;
import com.myiam.idm.core.services.RoleManagementService;
import com.myiam.idm.portal.helper.RoleModelToViewHelper;


/**
 * 
 * @author smeetsst
 *
 */
@Component("roleManager")
public class RoleManager implements Serializable {
	
	/*
	 * services 
	 */
	@Autowired
	private RoleManagementService roleManagementService;
	

	/*
	 * work variables
	 */
	
	private static final long serialVersionUID = 1L;
	
	private TreeNode root;
	private TreeNode selectedNode;  

	/*
	 * state
	 * 
	 * 
	 */
	private String roleName;
	private String roleType;
	private Role parentRole;
	private boolean showForm = false;
	private List<String> roleTypes;

	/* 
	 * constructors
	 */
	
	
	public RoleManager() {
		root = new DefaultTreeNode("root", null);
		

	    Role r0 = new Role(1, "root", "", null);
	    Role m0 = new Role(1, "Organisation", "org", r0);
	    Role m1 = new Role(1, "Administrative", "admin" , r0);
	    Role m0_1 = new Role(1, "Java", "org" , m0);
	    
	    root = RoleModelToViewHelper.roleModelToTree(r0);
	    
	    
	    roleTypes = new ArrayList<String>();
	    roleTypes.add("org");
	    roleTypes.add("admin");
	    
   	}
	
	
	/*
	 * actions
	 *
	 */
	
	
	public void submit(ActionEvent event) {
		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Correct", "Correct");
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	

    public void onNodeSelect(NodeSelectEvent event) {
    	String node = event.getTreeNode().getData().toString();

    		
    	selectedNode = event.getTreeNode();
    	
    	System.out.println("selected node " + node);
    	
    	}
    
    
    public void createNew () {
    	System.out.println("create new");
    	
    	if (selectedNode == null) {
    		// trow some msg back
    	} else {
    		parentRole = (Role) selectedNode.getData();  
    		showForm=true;
    	}
    }
    
    public void edit () {
    	System.out.println("edit");
    	showForm=true;
    }
    
    
    public void add(){
    	System.out.println("save " + roleName + " on " + selectedNode);
    	
    	Role newNode = new Role(0, roleName, "org" , (Role) selectedNode.getData());
    	parentRole.addChild(newNode);
    	
    	TreeNode node2 = new DefaultTreeNode(newNode, selectedNode);
    	
    	
    	roleManagementService.store(parentRole);
    	showForm=false;
    }

    

    /*
     * getters and setters
     */
    
	public TreeNode getRoot() {
		return root;
	}


    
    public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public List<String> getRoleTypes() {
		return this.roleTypes;
	}
	
	public boolean isShowForm(){
		return showForm;
	}
	
	public String getParentRoleName() {
		if (parentRole == null) {
			return "";
		}
		return parentRole.getName();
	}

    
    
}
